<template>
  <view class="flex-col page">
    <view
      class="flex-col group"
      :style="{ paddingTop: safeArea_top + 35 + 'px' }"
    >
      <view class="flex-row">
        <view v-if="isLogin" style="display: flex; align-items: center">
          <view class="image_3">
            <u-avatar size="45" :src="avatar"> </u-avatar>
          </view>
          <view class="flex-col group_2">
            <text class="text">{{ username }}</text>
			<view class="text_1">
				TEL：{{ mobile }} <text style="margin-left: 20rpx;">ID：{{ id }}</text>
			</view>
            <!-- <text >/text> -->
          </view>
        </view>

        <view
          v-else
          style="display: flex; align-items: center"
          @click="toLogin"
        >
          <view class="image_3">
            <u-avatar size="45" :src="avatar"> </u-avatar>
          </view>
          <view class="flex-col group_2">
            <text class="text">未登录.</text>
            <text class="text_1">请点击登录查看更多</text>
          </view>
        </view>
      </view>
      <view class="flex-col group_3">
        <view class="flex-col section_1">
          <view class="flex-row equal-division">
            <view
              class="flex-col equal-division-item"
              @click="toPath('/bundle/pages/wallet/wallet')"
            >
              <text class="text_4">我的余额(元)</text>
              <text class="text_5 price-font">{{ Number(balance) }}</text>
              <image
                src="https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/61ab188e1f655f0011bbaece/61e7d9270c0e3200129ec72c/16425844696329061808.png"
                class="image_5"
              />
            </view>
            <view
              class="flex-col equal-division-item_1"
              @click="toPath('/bundle/pages/coupon/coupon')"
            >
              <text class="text_6">我的优惠券</text>
              <view class="flex-row group_5">
                <text class="text_7 price-font">{{ coupon }}</text>
                <text class="text_8">张</text>
              </view>
              <image
                src="https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/61ab188e1f655f0011bbaece/61e7d9270c0e3200129ec72c/16425844697175691197.png"
                class="image_5"
              />
            </view>
          </view>
        </view>

        <view class="flex-col section_5 signGroup">
          <view class="flex justify-between flex1" style="align-items: center">
            <view class="tips">每日签到可获得<text>1至18元</text>现金充值</view>
            <view class="btn" @tap="toSign">立即签到</view>
          </view>
        </view>

        <view class="flex-col section_3">
          <view class="flex-row justify-bw group_6">
            <text class="text_9">我的订单</text>
            <view class="flex-row group_7" @click="toOrder('0')">
              <text>全部订单</text>
              <image
                src="https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/61ab188e1f655f0011bbaece/61e7d9270c0e3200129ec72c/16425844681219446616.png"
                class="image_8"
              />
            </view>
          </view>
          <view class="flex-row equal-division_1 justify-sa">
            <view
              class="equal-division-item_2 flex-col items-center"
              @click="toOrder('1')"
            >
              <image
                src="https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/61ab188e1f655f0011bbaece/61e7d9270c0e3200129ec72c/16425844699072572743.png"
                class="image_9"
              />
              <text class="text_11">待付款</text>
            </view>
            <view
              class="equal-division-item_2 flex-col items-center"
              @click="toOrder('2')"
            >
              <image
                src="https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/61ab188e1f655f0011bbaece/61e7d9270c0e3200129ec72c/16425844700094519484.png"
                class="image_9"
              />
              <text class="text_11">待确认</text>
            </view>
            <view
              class="equal-division-item_2 flex-col items-center"
              @click="toOrder('3')"
            >
              <image
                src="https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/61ab188e1f655f0011bbaece/61e7d9270c0e3200129ec72c/16425844700417096625.png"
                class="image_9"
              />
              <text class="text_11">进行中</text>
            </view>
            <view
              class="equal-division-item_2 flex-col items-center"
              @click="toOrder('4')"
            >
              <image
                src="https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/61ab188e1f655f0011bbaece/61e7d9270c0e3200129ec72c/16425844700880045855.png"
                class="image_9"
              />
              <text class="text_11">已完成</text>
            </view>
            <view
              class="equal-division-item_2 flex-col items-center"
              @click="toOrder('5')"
            >
              <image
                src="https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/61ab188e1f655f0011bbaece/61e7d9270c0e3200129ec72c/16425844702354452915.png"
                class="image_9"
              />
              <text class="text_11">已取消</text>
            </view>
          </view>
        </view>
        <view class="flex-col section_5">
          <text class="text_24">常用功能</text>
          <view class="grid">
            <view
              class="grid-item flex-col items-center"
              @click="toPath('/bundle/pages/user_ollection/user_ollection')"
            >
              <image
                src="https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/61ab188e1f655f0011bbaece/61e7d9270c0e3200129ec72c/16425844690553524351.png"
                class="image_9 center"
              />
              <text>我的收藏</text>
            </view>
            <view
              class="grid-item flex-col items-center"
              @click="toPath('/bundle/pages/wallet/wallet')"
            >
              <image
                src="https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/61ab188e1f655f0011bbaece/61e7d9270c0e3200129ec72c/16425844690662924855.png"
                class="image_9 center"
              />
              <text>我的钱包</text>
            </view>
            <view
              class="grid-item flex-col items-center"
              @click="toPath('/bundle/pages/feedback/feedback')"
            >
              <image
                src="https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/61ab188e1f655f0011bbaece/61e7d9270c0e3200129ec72c/16425844693155558501.png"
                class="image_9 center"
              />
              <text>意见反馈</text>
            </view>
            <view
              class="grid-item flex-col items-center"
              @click="toPath('/bundle/pages/user_address/user_address')"
            >
              <image
                src="https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/61ab188e1f655f0011bbaece/61e7d9270c0e3200129ec72c/16425844693642664924.png"
                class="image_9 center"
              />
              <text>地址管理</text>
            </view>
            <!-- <view
              v-if="license !== HUAWEI"
              class="grid-item flex-col items-center"
              @click="toPath('/bundle/pages/settled/settled')"
            >
              <image
                src="https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/61ab188e1f655f0011bbaece/61e7d9270c0e3200129ec72c/16425844690563034734.png"
                class="image_9 center"
              />
              <text>技师入驻</text>
            </view> -->

            <view class="grid-item flex-col items-center" @click="jiameng()">
              <image src="../../static/my/me7.png" class="image_9 center" />
              <text>招商加盟</text>
            </view>

            <view class="grid-item flex-col items-center" @click="call">
              <image
                src="https://codefun-proj-user-res-1256085488.cos.ap-guangzhou.myqcloud.com/61ab188e1f655f0011bbaece/61e7d9270c0e3200129ec72c/16425844692152240400.png"
                class="image_9 center"
              />
              <text>联系客服</text>
            </view>

            <view class="grid-item flex-col items-center" @click="complaint">
              <view class="image_9 center" style="padding-top: 15rpx">
                <uni-icons type="info" size="35"></uni-icons>
              </view>
              <text>投诉管理</text>
            </view>
          </view>
        </view>
      </view>
    </view>
    <up-popup
      :show="authGetTips"
      mode="top"
      :closeOnClickOverlay="false"
      round="14"
    >
      <view
        style="
          width: 100%;
          padding: 20px 20px 10px;
          display: flex;
          align-items: center;
        "
      >
        <view style="margin-right: 30rpx; display: flex; align-items: center">
          <up-icon name="map" size="30"></up-icon>
        </view>
        <view style="display: flex; flex-direction: column">
          <view style="font-weight: 500">电话权限使用说明</view>
          <view style="font-size: 26rpx">帮助您解决订单上遇到的问题！</view>
        </view>
      </view>
    </up-popup>
  </view>
</template>

<script>
import { HUAWEI } from "@/utils/enum.js";
export default {
  data() {
    return {
      HUAWEI,
      id: "",
      username: "",
      mobile: "",
      is_vip: false,
      overdue_time: "",
      avatar: "",
      sex: 0,
      balance: "",
      collect: 0,
      coupon: 0,
      news: 0,
      unpaid: 0,
      unpaid_order: null,
      end_t: "",
      mobiles: "",
      isVip: 0,
      is_vip: 0,
      is_ios: 1,
      new_people: 0,
      isLogin: false,
      safeArea_top: 0,
      license: null,
      authGetTips: false,
    };
  },
  onLoad() {
    this.license = uni.getStorageSync("app-license");
  },
  onShow() {
    const safeArea = uni.getStorageSync("safeArea");
    this.safeArea_top = safeArea.top;
    this.isLogin = uni.getStorageSync("userInfo");
    if (this.isLogin) {
      this.init();
    }
    this.$http.post("api/index/shareConfig").then((res) => {
      const { is_vip } = res;
      this.isVip = is_vip;
    });
    this.is_ios = uni.getStorageSync("is_ios");
  },
  onNavigationBarButtonTap: function (e) {
    if (!this.isLogin) {
      this.toLogin();
      return;
    }
    let { index } = e;
    if (index === 1) {
      uni.navigateTo({
        url: "/bundle/pages/message/message",
      });
    }
    if (index === 0) {
      uni.navigateTo({
        url: "/bundle/pages/user_profile/user_profile",
      });
    }
  },
  methods: {
    complaint() {
      uni.navigateTo({
        url: "/pages/my/complaint",
      });
    },
    toSign() {
      const isLogin = uni.getStorageSync("userInfo");
      if (isLogin) {
        // uni.showLoading({});
        this.$http.post("api/user/sign").then((res) => {
          let icon = "error";
          if (res.code == 1) {
            icon = "success";
            this.init();
          }
          // uni.hideLoading();
          uni.showToast({
            icon,
            title: res.msg,
          });
        });
      } else {
        uni.showToast({
          icon: "none",
          title: "请先登录",
        });
      }
    },
    toLogin() {
      // uni.setStorageSync("my-back", '/pages/my/my')
      uni.navigateTo({
        url: "/pages/user/login22",
      });
    },
    toOrderDetail(e) {
      uni.navigateTo({
        url: "/bundle/pages/order_details/order_details?order_sn=" + e.order_sn,
      });
    },
    showtime(endtime) {
      var nowtime = new Date(), //获取当前时间
        endtime = new Date(endtime); //定义结束时间
      var lefttime = endtime.getTime() - nowtime.getTime(), //距离结束时间的毫秒数
        leftd = Math.floor(lefttime / (1000 * 60 * 60 * 24)), //计算天数
        lefth = Math.floor((lefttime / (1000 * 60 * 60)) % 24), //计算小时数
        leftm = Math.floor((lefttime / (1000 * 60)) % 60), //计算分钟数
        lefts = Math.floor((lefttime / 1000) % 60); //计算秒数
      if (lefttime <= 0) {
        return "已过期";
      }
      return (
        this.add0(leftd) +
        "天" +
        this.add0(lefth) +
        ":" +
        this.add0(leftm) +
        ":" +
        this.add0(lefts)
      ); //返回倒计时的字符串
    },
    add0(a) {
      a = Number(a);
      return a < 10 ? "0" + a : a;
    },
    init() {
      // 获取个人信息
      this.$http.post("api/user/userInfo").then((res) => {
        for (let i in res) {
          if (i === "unpaid_order") {
            if (res[i] && JSON.stringify(res[i]) !== "{}") {
              this[i] = res[i];
            }
          } else {
            this[i] = res[i];
          }
        }
        uni.setStorageSync("new_people", res.new_people);
      });
      // 获取客服信息 api/user/customerService
      let location = uni.getStorageSync("location");
      if (location) {
        location = location;
      } else {
        location = {
          lat: 0,
          lng: 0,
        };
      }
      this.$http
        .post(
          `api/user/customerService?lat=${location.lat}&lng=${location.lng}`
        )
        .then((res) => {
          let { mobile } = res;
          this.mobiles = mobile;
        });
    },
    toPath(url) {
      if (this.isLogin) {
        uni.navigateTo({
          url,
        });
      } else {
        this.toLogin();
      }
    },
    toOrder(index) {
      uni.setStorageSync("tabsIndex", index);
      uni.reLaunch({
        url: "/pages/order/order",
      });
    },
    call() {
      const makePhone = uni.getStorageSync("make-phone");
      if (this.mobiles) {
        const mobile = this.mobiles;
        uni.showModal({
          content: "是否拨打客服电话",
          success: (res) => {
            if (res.confirm) {
              if (!makePhone) {
                this.authGetTips = true;
              }
              uni.makePhoneCall({
                phoneNumber: mobile + "",
                success: (res) => {
                  uni.setStorageSync("make-phone", true);
                },
                // 失败回调
                fail: (res) => {
                  // console.log("调用失败!", res);
                },
                complete: () => {
                  setTimeout(() => {
                    this.authGetTips = false;
                  }, 5000);
                },
              });
            }
          },
        });
        return;
      }
      uni.showToast({
        icon: "none",
        title: "暂无客服联系号码",
      });
    },
    jiameng() {
      plus.runtime.openWeb("https://www.wenjuan.com/s/UZBZJvtJqvz/");
    },
  },
};
</script>

<style scoped lang="scss">
@import './my.scss';
</style>
